package com.uenpay.im.ui.old.activity.nearby;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.entities.QYXUserEntity;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class GetNearByPeopleInvokeItem extends BaseHttpInvokeItem {
	public class GetNearByPeopleInvokeItemResult extends HttpInvokeResult {
		public int status;
		public String msg;
		public ArrayList<QYXUserEntity> arrayList = new ArrayList<QYXUserEntity>();
	}

	public GetNearByPeopleInvokeItemResult getOutput() {
		return (GetNearByPeopleInvokeItemResult) GetResultObject();
	}

	public GetNearByPeopleInvokeItem(String longitude, String latitude,int sex) {
		String url = APIConfiguration.getBaseUrl() + "Nearby/Nearby/search?"
				+ APIConfiguration.getCustIdAndToken();
		HashMap<String, String> mRequestParams = new HashMap<String, String>();
		mRequestParams.put("longitude", longitude);
		mRequestParams.put("latitude", latitude);
		if(sex != -1){
			mRequestParams.put("sex", sex+"");
		}
		SetRequestParams(mRequestParams);
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetNearByPeopleInvokeItemResult result = new GetNearByPeopleInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status != 0) {
			return result;
		}
		JSONArray jsonArray = json.optJSONArray("data");
		if(jsonArray == null)return result;
		for(int i =0,size = jsonArray.length(); i < size; i++){
			result.arrayList.add(desQYXUserEntity(jsonArray.optJSONObject(i)));
		}
		return result;
	}
	
	private QYXUserEntity desQYXUserEntity(JSONObject json){
		QYXUserEntity entity = new QYXUserEntity();
		entity.nickname = json.optString("nickname");
		entity.idenum = json.optString("geodistance");
		entity.realname = json.optString("signature");
		entity.custid = json.optString("custid");
		entity.sex = json.optInt("sex");
		return entity;
	}
}
